/*
 * VHEternalStandingsGrid.java
 * Created on 15 juil. 2009
 *
 * VirtualHockey Internet Platform
 * http://code.google.com/p/virtualhockey/
 *
 * Copyright (c) 2005-2009, Jens and Jan Janke (VirtualHockey Project)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
 */

package com.hockeo.vhweb.client.ui.widget;

import java.util.Map;

import com.extjs.gxt.ui.client.Style;
import com.extjs.gxt.ui.client.data.BasePagingLoadConfig;
import com.extjs.gxt.ui.client.data.BasePagingLoader;
import com.extjs.gxt.ui.client.data.PagingLoadConfig;
import com.extjs.gxt.ui.client.data.PagingLoadResult;
import com.extjs.gxt.ui.client.data.PagingLoader;
import com.extjs.gxt.ui.client.data.RpcProxy;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.toolbar.PagingToolBar;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.hockeo.vhweb.client.mvc.VHController;
import com.hockeo.vhweb.client.utils.VHJsData;

/**
 * Special grid for eternal standings. Supports loading eternal standings with paging
 * support from the server.
 *
 * @version $Id$
 * @author jjanke
 */
public class VHEternalStandingsGrid extends VHStandingsGrid
{
  private static final int                         PAGE_SIZE = 20;

  private PagingLoader<PagingLoadResult<VHJsData>> d_loader;
  private PagingToolBar                            d_pagingToolBar;

  /**
   * Creates a new grid for eternal standings.
   *
   * @param strElementId the ID of the contest element for which to display eternal
   *          standings
   */
  public VHEternalStandingsGrid( VHController controller, String strElementId )
  {
    super( controller, VHStandingsGrid.DisplayType.ETERNAL, strElementId );

    // customise the grid for paging
    //
    final Grid<VHJsData> grid = getGrid();

    grid.setStateId( "eternalStandingsGrid" );
    grid.setStateful( true );
    grid.addListener( Events.Attach, new Listener<GridEvent<VHJsData>>() {
      @SuppressWarnings( "synthetic-access" )
      public void handleEvent( GridEvent<VHJsData> be )
      {
        PagingLoadConfig config = new BasePagingLoadConfig();
        config.setOffset( 0 );
        config.setLimit( PAGE_SIZE );

        Map<String, Object> state = grid.getState();
        if ( state.containsKey( "offset" ) )
        {
          int offset = (Integer) state.get( "offset" );
          int limit = (Integer) state.get( "limit" );
          config.setOffset( offset );
          config.setLimit( limit );
        }
        if ( state.containsKey( "sortField" ) )
        {
          config.setSortField( (String) state.get( "sortField" ) );
          config.setSortDir( Style.SortDir.valueOf( (String) state.get( "sortDir" ) ) );
        }

        d_loader.load( config );
      }
    } );
  }

  @Override
  protected ListStore<VHJsData> createStore()
  {
    RpcProxy<PagingLoadResult<VHJsData>> proxy = new RpcProxy<PagingLoadResult<VHJsData>>() {
      @Override
      protected void load( Object objCfg, AsyncCallback<PagingLoadResult<VHJsData>> callback )
      {
        VHController.getBaseDataService().getEternalStandings( (PagingLoadConfig) objCfg, VHController.getLocale(),
            getElementId(), callback );
      }
    };

    d_loader = new BasePagingLoader<PagingLoadResult<VHJsData>>( proxy );
    d_loader.setRemoteSort( true );

    d_pagingToolBar = new PagingToolBar( PAGE_SIZE );
    d_pagingToolBar.bind( d_loader );
    d_pagingToolBar.setBorders( true );

    return new ListStore<VHJsData>( d_loader );
  }

  /**
   * Returns the paging toolbar.
   */
  public final PagingToolBar getPagingToolBar()
  {
    return d_pagingToolBar;
  }
}
